﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace telWebBrowserNet.Lib.TeleDLL.Model
{
    /// <summary>
    /// 写实体类
    /// </summary>
    public class WriteModel
    {
        int pDeviceHandle;
        /// <summary>
        /// 设备的句柄
        /// </summary>
        public int PDeviceHandle { get => pDeviceHandle; set => pDeviceHandle = value; }

        uint dwTimeoutMs;
        /// <summary>
        /// 读取超时时间，毫秒
        /// </summary>
        public uint DwTimeoutMs { get => dwTimeoutMs; set => dwTimeoutMs = value; }

        /// <summary>
        /// 命令集
        /// </summary>
        private Dictionary<string, string> dicCommands;

        /// <summary>
        /// 构造方法
        /// </summary>
        public WriteModel()
        {
            dicCommands = new Dictionary<string, string>();
            dwTimeoutMs = 1500;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="deviceHandle"></param>
        public WriteModel(int deviceHandle)
        {
            dicCommands = new Dictionary<string, string>();
            dwTimeoutMs = 1500;
            pDeviceHandle = deviceHandle;
        }

        /// <summary>
        /// 添加命令
        /// </summary>
        /// <param name="commandName">命令名，如：CallOutNumber</param>
        /// <param name="commandValue">命令值 ，如13800138000，如果只有命令名，没有命令值，如发CurrentStation?，则命令值为空字符串即可</param>
        public void addCommand(string commandName, string commandValue)
        {
            dicCommands[commandName] = commandValue;
        }

        public bool removeCommand(string commandName)
        {
            return dicCommands.Remove(commandName);
        }

        /// <summary>
        /// 获取命令内容
        /// </summary>
        /// <param name="commandName">命令名</param>
        /// <returns></returns>
        public string getCommand(string commandName)
        {
            if (!dicCommands.ContainsKey(commandName))
            {
                return "";
            }
            return dicCommands[commandName];
        }

        /// <summary>
        /// 生成指令
        /// </summary>
        /// <returns>最终生成指令如：adusun<CurrentStation?;checksum:000000>end</returns>
        public string GetOrderStr()
        {
            //LogInfo.LogOutput(LogInfo.log_info, "生成指令-top");
            StringBuilder sb = new StringBuilder();
            sb.Append("adusun<");
            //通过键的集合取
            string dot = "";
            foreach (string key in dicCommands.Keys)
            {
                sb.Append(dot);
                sb.Append(key);
                if (dicCommands[key] != "")
                {
                    sb.Append(":");
                    sb.Append(dicCommands[key]);//如果命令值不为空，则加上
                }
                dot = ";";
            }
            sb.Append(">end");
            //LogInfo.LogOutput(LogInfo.log_info, "生成指令-bottom");
            return sb.ToString();
        }

        /// <summary>
        /// 清空命令集
        /// </summary>
        public void ClearCommand()
        {
            dicCommands.Clear();
            LogInfo.LogOutput(LogInfo.log_info, "清空命令集");
        }
    }
}
